﻿namespace Vdc.Grid
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class FieldsFiller
    {
        private XmlNodes fieldNodes;
        private Localization local;

        public FieldsFiller(Page page, string name, string path, string lang)
        {
            string str = path.Replace(@"\", "/");
            this.local = new Localization(str, lang, page, true);
            XmlNodes nodes = this.local.getCollection(this.local.getCollection(null, "popup-windows")[0], "popup");
            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Attributes["name"].Value.ToLower() == name.ToLower())
                {
                    this.fieldNodes = this.local.getCollection(nodes[i], "field");
                    return;
                }
            }
        }

        public void Fill(Label label, string name)
        {
            if (this.fieldNodes != null)
            {
                for (int i = 0; i < this.fieldNodes.Count; i++)
                {
                    if (this.fieldNodes[i].Attributes["name"].Value.ToLower() == name.ToLower())
                    {
                        label.Text = this.fieldNodes[i].Attributes["value"].Value;
                        return;
                    }
                }
            }
        }

        public string Get(string name, string dflt)
        {
            if (this.fieldNodes != null)
            {
                for (int i = 0; i < this.fieldNodes.Count; i++)
                {
                    if (this.fieldNodes[i].Attributes["name"].Value.ToLower() == name.ToLower())
                    {
                        return this.fieldNodes[i].Attributes["value"].Value;
                    }
                }
            }
            return dflt;
        }
    }
}

